package com.hlingsoft.xinsuo.action;

import com.hlingsoft.xinsuo.dao.LockDao;
import com.hlingsoft.xinsuo.dao.VerifyCodeDao;
import com.hlingsoft.xinsuo.entity.Lock;
import com.hlingsoft.xinsuo.entity.VerifyCode;
import com.hlingsoft.xinsuo.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by selbylei on 2015/10/16.
 */
@Controller
@RequestMapping("/lock")
public class LockAction {

    @Autowired
    private LockDao lockDao;

    @Autowired
    private VerifyCodeDao verifyCodeDao;


    /**
     * 上锁
     * @param phone1
     * @param phone2
     * @param verifyCode1
     * @param verifyCode2
     * @param devType
     * @param request
     * @return
     */
    @RequestMapping(value = "/getOn.json")
    @ResponseBody
    public Map<String, Object> getOn(
            @RequestParam(value = "phone1") String phone1,         //手机号1
            @RequestParam(value = "phone2",required = false) String phone2,         //手机号2
            @RequestParam(value = "verifyCode1",required = false) String verifyCode1,   //密码1
            @RequestParam(value = "verifyCode2",required = false) String verifyCode2,  //密码2
            @RequestParam(value = "devType",required = false) String devType,      //设备类型
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            VerifyCode vc1 = verifyCodeDao.getVerifyCodeByPhone(phone1);
            if (vc1!=null){
                if (verifyCode1.equals(vc1.getCode())){
                    resultMap.put("msg1","第一步注册通过");
                    resultMap.put("result1",true);
                    if (phone2!=null){
                        VerifyCode vc2 = verifyCodeDao.getVerifyCodeByPhone(phone2);
                        if (vc2!=null){
                           if (verifyCode2.equals(vc2.getCode())){
                               Lock lock = new Lock();
                               Date now = new Date();
                               lock.setPhone1(phone1);
                               lock.setPhone2(phone2);
                               lock.setDevType(devType);
                               lock.setState(0);
                               lock.setCreateTime(now);
                               lock.setUpdateTime(now);
                               lockDao.add(lock);
                               lock.setDevId(lock.getId());
                               lockDao.updateLock(lock);
                               resultMap.put("lock",lock);
                               resultMap.put("msg2","注册成功");
                               resultMap.put("result2",true);
                           }else {
                               resultMap.put("msg2","验证码输入有误");
                               resultMap.put("result2",false);
                           }
                        }else {
                            resultMap.put("msg2","请先获取验证码");
                            resultMap.put("result2",false);
                        }
                    }
                }else {
                    resultMap.put("msg1","验证码输入有误");
                    resultMap.put("result1",false);
                }
            }else {
                resultMap.put("msg1","验证码未获取");
                resultMap.put("result1",false);
            }

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 确认是否加锁成功
     * @param lockId
     * @param state
     * @param request
     * @return
     */
    @RequestMapping(value = "/checkGetOn.json")
    @ResponseBody
    public Map<String, Object> checkGetOn(
            @RequestParam(value = "lockId") long lockId,      //设备类型
            @RequestParam(value = "state") int state,         //0.设备上锁未成功 1.设备上锁成功，2.设备拆锁
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
             Lock lock = lockDao.findById(lockId);
             if (lock!=null){
                 if (state==1||state==0){
                     lock.setState(state);
                     lockDao.updateLock(lock);
                     resultMap.put("msg","状态更新成功");
                     resultMap.put("result",true);
                 }
             }else {
                 resultMap.put("msg","lockId参数值有误");
                 resultMap.put("result",false);
             }
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 我的心锁
     *
     * @param phone
     * @param request
     * @return
     */
    @RequestMapping(value = "/getList.json")
    @ResponseBody
    public Map<String, Object> getList(
            @RequestParam(value = "phone") String phone,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {

            List<Lock> lockList = new ArrayList<Lock>();

            SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");

            Date  now = new Date();
            List<Lock> joList = lockDao.findByDevTypeAndPhoneAndState("jiaou", phone, 1);
            for (int i = joList.size(); i>0 ; i--) {
                  long diff = now.getTime() - joList.get(i-1).getCreateTime().getTime();
                  long diffDays = diff / (1000 * 60 * 60 * 24);
                  joList.get(i-1).setDiffDays(diffDays);
                  joList.get(i-1).setDevName("佳藕"+(joList.size()-i+1)+"  相锁时间 "+format.format(joList.get(i-1).getCreateTime()));
            }

            List<Lock> ryList = lockDao.findByDevTypeAndPhoneAndState("ruyi", phone, 1);
            for (int i = ryList.size(); i>0 ; i--) {
                long diff = now.getTime() - ryList.get(i-1).getCreateTime().getTime();
                long diffDays = diff / (1000 * 60 * 60 * 24)+1;
                ryList.get(i-1).setDiffDays(diffDays);
                ryList.get(i-1).setDevName("如意" + (ryList.size()-i+1) + "  相锁时间 " + format.format(ryList.get(i-1).getCreateTime()));
            }

            lockList.addAll(joList);
            lockList.addAll(ryList);
            resultMap.put("lockList", lockList);
            resultMap.put("result", true);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 解锁
     * @param lockId
     * @param request
     * @return
     */
    @RequestMapping(value = "/getOff.json")
    @ResponseBody
    public Map<String, Object> getOn(
            @RequestParam(value = "lockId") long lockId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Lock lock = lockDao.findByIdAndState(lockId, 1);

            if (lock!=null){
                Date now = new Date();
                lock.setUnlockTime(now);
                lock.setState(2);        //state:2申请解锁
                lockDao.updateLock(lock);
                resultMap.put("msg","解锁成功");
                resultMap.put("result",true);
            }  else {
               resultMap.put("msg","心锁不在锁定状态");
                resultMap.put("result",false);
            }

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 获取拆锁时间
     * @param lockId
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUnlockTime.json")
    @ResponseBody
    public Map<String, Object> getUnlockTime(
            @RequestParam(value = "lockId") long lockId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Lock lock = lockDao.findByIdAndState(lockId, 2);
            if (lock!=null){
                Calendar cal=Calendar.getInstance();
                cal.setTime(lock.getUnlockTime());
                String unlockTime = cal.get(Calendar.YEAR)+"年"+(cal.get(Calendar.MONTH) + 1)+"月"+cal.get(Calendar.DAY_OF_MONTH)+"日";
                resultMap.put("unlockTime", unlockTime);
                resultMap.put("result",true);
            }  else {
                resultMap.put("msg","心锁未拆锁或不存在");
                resultMap.put("result",false);
            }

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


}
